@extends('layouts.admin')
@section('content')
    <div id="ribbon"><ol class="breadcrumb"><li>内容区</li></ol></div>
    <div class="content_wrapper">
        <aside>
            <header><i class="fa fa-fw fa-file"></i>添加模块商品</header>
            <section>
                <div class="row">
                    <div class="col-sm-12">
                        <form id="dataForm" onsubmit="return false;" class="form-horizontal" role="form">
                            <input type="hidden" name="module_id" value="{{request('module_id')}}">
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><span class="red">*</span>店铺</label>
                                <div class="col-sm-3">
                                    <select name="agent_id" class="form-control" onchange="loadGoods(this.value)">
                                        <option value="">请选择店铺</option>
                                        @foreach($agents as $k => $v)
                                            <option value="{{$k}}">{{$v}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"><span class="red">*</span>商品</label>
                                <div class="col-sm-3">
                                    <select name="goods_id" class="form-control" id="goods_id">
                                    </select>
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">序号</label>
                                <div class="col-sm-3">
                                    <input type="number" name="order_num" value="0" class="form-control" maxlength="20">
                                </div>
                            </div>
                            <hr/>
                            <div class="form-group">
                                <label class="col-sm-2 control-label"> </label>
                                <div class=" col-sm-10">
                                    <input type="button" class="btn btn-primary" value="保存" id="saveBtn" onclick="save()" />&nbsp;&nbsp;&nbsp;
                                    <input type="button" class="btn btn-default" value="返回" onclick="history.go(-1)" />
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </section>
        </aside>
    </div>
    <script>
        function save() {
            $.post('', $('#dataForm').serialize(), function (res) {
                if (res.status === 1) {
                    layer.msg(res.info, {icon: res.status}, function () {
                        location.href=document.referrer;
                    });
                } else {
                    layer.msg(res.info, {icon: res.status});
                }
            });
        }

        function loadGoods(agent_id) {
            $.post('{{url('admin/goods/getGoodsByAgentId ')}}', {agent_id: agent_id}, function (res) {
                var htmlContent = '<option value="">请选择商品</option>';
                for (var i = 0; i < res.length; i++) {
                    htmlContent += '<option value="' + res[i].id + '">' + res[i].name + '</option>';
                }
                $('#goods_id').html(htmlContent);
            });
        }
    </script>
@endsection